<template>
  <div class="qingwu">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{info.keyword_id?'修改关键词':'添加关键词'}}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="el-icon-back" @click="$router.go(-1)">返回</el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="150px" ref="info" :model="info">
          <el-form-item label="关键词名称" prop="name" :rules="[{required:true,message:'关键词名称不能为空',trigger: 'blur' }]">
            <el-input v-model="info.name"></el-input>
          </el-form-item>
          <el-form-item label="关键词描述" prop="desc" :rules="[{required:true,message:'关键词描述不能为空',trigger: 'blur' }]">
            <el-input v-model="info.desc"></el-input>
          </el-form-item>
          <el-form-item label="关联类型" prop="type" :rules="[{required:true,message:'请选择关联类型',trigger: 'change' }]">
            <el-select v-model="info.type" placeholder="请选择类型">
              <el-option label="商品" :value="1"></el-option>
              <el-option label="项目" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item :label="info.type==2?'APP首页推荐':'商城首页推荐'" prop="type" :rules="[{required:true,message:'请选择APP首页推荐',trigger: 'blur' }]">
            <el-radio-group v-model="radio">
              <el-radio :label="0">是</el-radio>
              <el-radio :label="1">否</el-radio>
            </el-radio-group>
          </el-form-item> -->
          <el-form-item label="排序" prop="sort" :rules="[{required:true,message:'排序不能为空',trigger: 'blur' }]">
            <el-input v-model="info.sort"></el-input>
            <!-- <el-input :disabled="radio==1" v-model="info.sort"></el-input> -->
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('info')">提交</el-button>
            <el-button @click="resetForm('info')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      info: {
        name: "",
        desc: "",
        type: 1,
        sort: 0,
        keyword_id: 0,
      },
      // radio:0,
    };
  },
  watch: {},
  computed: {},
  methods: {
    resetForm: function (e) {
      this.$refs[e].resetFields();
    },
    isEdit() {
        return this.info.keyword_id > 0
    },
    submitForm: function (e) {
      let _this = this;

      // 验证表单
      this.$refs[e].validate(function (res) {
        if (res) {
          // Http 请求
          let url = _this.isEdit() ? _this.$api.keywordEdit : _this.$api.keywordAdd;
          if(_this.isEdit()) delete _this.info.keywordAdd
          _this.$post(url, _this.info).then(function (res) {
            if (res.code == 200) {
              _this.$message.success(_this.isEdit()?"编辑成功":"添加成功");
              _this.$router.go(-1);
            } else {
              _this.$message.error(_this.isEdit()?"编辑失败":"添加失败");
            }
          });
        }
      });
    },
    get_keyword_info: function () {
      let _this = this;
      this.$get(this.$api.keywordShow,{keyword_id:this.info.keyword_id}).then(function (res) {
        _this.info = Object.assign(_this.info,res.data);
      })

    }
  },
  created() {
    // 判断是否是编辑
    if (!this.$isEmpty(this.$route.params.id)) {
      this.info.keyword_id = this.$route.params.id;
    }
  },
  mounted() { 
    if (this.info.keyword_id > 0) {
      this.get_keyword_info();
    }
  }
};
</script>
<style lang="scss" scoped>
</style>